function linkNodes(father, son, aPoint)
{
    if ( father === null || son === null ) return false;

    var linkobj = son.link;

    if ( linkobj !== null )
    {
        linkobj.a = father;
        linkobj.b = son;
        linkobj.aPoint = aPoint;
        linkobj.parent = son;
        linkobj.objectName = son.objectName + "_link";
    }
    else
        console.log("[TreeUtil.js] node " + son.objectName + " has no link")

    return linkobj;
}

var nodecomp = null;
var nodeCount = 0;

function createNode(field, father, nodesize)
{
    var nodeobj = null;

    if (nodecomp === null)
        nodecomp = Qt.createComponent("Node.qml");

    if ( nodecomp.status == Component.Ready )
    {
        nodeobj = nodecomp.createObject(field);
        nodeobj.objectName = "node" + nodeCount;
        nodeCount++;

        if ( nodeobj === null )
        {
            console.log("error creating component");
            console.log(component.errorString());
            return false;
        }
        nodeobj.x = 0;
        nodeobj.y = 0;

        nodeobj.itemCount = nodesize;
        nodeobj.width = 4 + nodeobj.itemWidth * nodeobj.itemCount;
        nodeobj.height = Math.ceil(field.height / 20);
        nodeobj.z = 3;

        addPoints(nodeobj);
    }
    else
    {
        console.log("error loading component");
        console.log(itemcomp.errorString());
        return false;
    }

    return nodeobj;
}

var pointcomp = null;

function addPoints(node)
{
    if ( node === null || node.itemCount < 1 )
        return false;

    if ( pointcomp === null )
        pointcomp = Qt.createComponent("ConnectionPoint.qml");

    if ( pointcomp.status == Component.Ready )
    {
        var pointobj = null;

        for ( var i = 0; i <= node.itemCount; i++ )
        {
            pointobj = pointcomp.createObject(node);
            pointobj.objectName = node.objectName + "_point" + i;
            pointobj.y = node.height - pointobj.height;

            pointobj.x = i*node.itemWidth //- pointobj.width / 2;
        }
    }
    else
    {
        console.log("error loading component");
        console.log(pointcomp.errorString());
        return false;
    }
    return true;
}
